package com.gxa.insurance.service.impl;

import com.gxa.insurance.exception.BusinessException;
import com.gxa.insurance.exception.ExceptionCode;
import com.gxa.insurance.mapper.ExpressRepairShopMapper;
import com.gxa.insurance.pojo.ExpressRepairShop;
import com.gxa.insurance.service.ExpressRepairShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wang li sha
 * @date 2022-06-06 16:33
 */
@Service("ExpressRepairShopService")
@Transactional(rollbackFor = Throwable.class)
public class ExpressRepairShopServiceImpl implements ExpressRepairShopService {
    @Autowired
    ExpressRepairShopMapper expressRepairShopMapper;

    @Override
    public Integer insert(ExpressRepairShop expressRepairShop) throws Exception {
        Integer insert = expressRepairShopMapper.insert(expressRepairShop);
        if (insert != 1) {
            throw new BusinessException(ExceptionCode.INSERT_EXPRESSREPAIRSHOP_FAILED);
        }
        return insert;
    }

    @Override
    public Integer update(ExpressRepairShop expressRepairShop) throws Exception {
        Integer update = expressRepairShopMapper.update(expressRepairShop);
        if (update != 1) {
            throw new BusinessException(ExceptionCode.UPDATE_EXPRESSREPAIRSHOP_FAILED);
        }
        return update;
    }

    @Override
    public Integer deleteById(Integer id) throws Exception {
        Integer delete = expressRepairShopMapper.deleteById(id);
        if (delete != 1) {
            throw new BusinessException(ExceptionCode.DELETE_EXPRESSREPAIRSHOP_FAILED);
        }
        return delete;
    }

    @Override
    public ExpressRepairShop selectById(Integer id) throws Exception {
        ExpressRepairShop expressRepairShop = expressRepairShopMapper.selectById(id);
        if (expressRepairShop == null) {
            throw new BusinessException(ExceptionCode.EXPRESSREPAIRSHOP_NOT_FONUD);
        }
        return expressRepairShop;
    }

    @Override
    public List<ExpressRepairShop> selectAll() throws Exception {
        List<ExpressRepairShop> expressRepairShops = expressRepairShopMapper.selectAll();
        if (expressRepairShops.size() == 0) {
            throw new BusinessException(ExceptionCode.EXPRESSREPAIRSHOP_IS_EMPTY);
        }
        return expressRepairShops;
    }


    @Override
    public List<ExpressRepairShop> selectAllByPage(Integer page, Integer size) throws Exception {
        List<ExpressRepairShop> expressRepairShops = expressRepairShopMapper.selectAllByPage((page - 1) * size, size);
        if (expressRepairShops.size() == 0) {
            throw new BusinessException(ExceptionCode.EXPRESSREPAIRSHOP_IS_EMPTY);
        }
        return expressRepairShops;
    }

    @Override
    public List<ExpressRepairShop> selectLikeName(String name) throws Exception {
        List<ExpressRepairShop> expressRepairShops = expressRepairShopMapper.selectLikeName(name);
        if (expressRepairShops.size() == 0) {
            throw new BusinessException(ExceptionCode.EXPRESSREPAIRSHOP_IS_EMPTY);
        }
        return expressRepairShops;
    }

    @Override
    public List<ExpressRepairShop> selectLikeNameByPage(Integer page, Integer size, String name) throws Exception {
        List<ExpressRepairShop> expressRepairShops = expressRepairShopMapper.selectLikeNameByPage((page - 1) * size, size, name);
        if (expressRepairShops.size() == 0) {
            throw new BusinessException(ExceptionCode.EXPRESSREPAIRSHOP_IS_EMPTY);
        }
        return expressRepairShops;
    }
}
